package memberrewards.service;

import hackathon1212.ServletPojoAdapter;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import memberrewards.delegate.RewardsDelegate;
import memberrewards.dtos.AccountDetailDTO;
import memberrewards.dtos.BonusOfferDTO;


public class BonusOffers extends ServletPojoAdapter {

	private static final long serialVersionUID = -8655087464272479429L;

	protected Object getBusinessMethod(Map<String, String[]> params){
		//Query Params
		final String pan = params.get("pan")!=null ? params.get("pan")[0]:null;

		//Default response
		final Map<String,Object> ret = new HashMap<String, Object>();
		final List<Map<String,String>> bonusoffers = new LinkedList<Map<String,String>>();
		ret.put("bonusoffers", bonusoffers);
		ret.put("status", "Card Not Found");
		
		final AccountDetailDTO accountDetail = RewardsDelegate.getAccountDetail(pan);
		if (accountDetail!=null) {
			final List<BonusOfferDTO> items = RewardsDelegate
					.getEligibleBonusOffers(accountDetail.getTier());

				for (BonusOfferDTO item : items) {	
					//keep the Order
					final Map<String,String> bonusoffer = new LinkedHashMap<String, String>();
					bonusoffer.put("bonusid", item.getOfferId());
					bonusoffer.put("bonustype", item.getOfferType());
					bonusoffer.put("eligiblemerchant", item.getEligibleMerchants());
					bonusoffer.put("description", item.getDescription());
					bonusoffers.add(bonusoffer);
				}
				ret.put("status", "Success");
		}

		return ret;
	}
}
